var lodash = require('lodash');
import { Dao } from "./dao-service";

function Updates() {
    Dao.apply(this, ['updates']);
}

Updates.prototype.saveUpdateUrls = function (result, useLDCTime) {
    var self = this;
    return Promise.all(result.urls.map(function (url) {
        if (url.updateId) {
            if (useLDCTime) {
                url.timeWhen = new Date().getTime();
            }
            return self.datastore.update(url).then(function () {
                return url;
            });
        }
    })).then(function (urls) {
        return lodash.without(urls, undefined);
    });
};

Updates.prototype.queryAll = function () {
    return this.datastore.find({});
};

module.exports = new Updates();